package com.tianpengcc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {

	@Override
	public void destroy() {
		// TODO 自动生成的方法存根

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)servletRequest ;
		HttpServletResponse response = (HttpServletResponse) servletResponse ;
		HttpSession session = request.getSession();
		
		String servletPath = request.getServletPath();
		System.out.println(servletPath);
		if(!(servletPath.indexOf("/index") != -1 || servletPath.indexOf("/login") != -1 || servletPath.indexOf("/style") != -1
				|| servletPath.indexOf("/css") != -1 || servletPath.indexOf("/images") != -1
				|| servletPath.indexOf("/img") != -1 || servletPath.indexOf("/js") != -1)){
			if(session.getAttribute("user") == null){
				response.sendRedirect(request.getContextPath() + "/index");
				return ;
			}
		}
		
		chain.doFilter(servletRequest, servletResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO 自动生成的方法存根

	}

}
